package com.example.conf.cors;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter(){
        CorsConfiguration conf = new CorsConfiguration();
        //接受任意域名的请求
        conf.addAllowedOrigin("*");
        //不支持提交COOKIE数据
        conf.setAllowCredentials(false);
        //绑定请求头信息,使用通配符* 接受任意字段
        conf.addAllowedHeader("*");
        //支持 任意方法提交方法
        conf.addAllowedMethod("*");
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**",conf);
        return new CorsFilter(configurationSource);
    }
}
